//
//  GJNumberAnnotationCallOut.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/8/31.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJNumberAnnotationCallOut.h"

#define themColor  HexColor(#BA914A)

static const CGFloat Arror_height = 5;


@interface GJNumberAnnotationCallOut ()
@property (nonatomic, strong)UILabel *numberLabel;
@property (nonatomic, strong)UILabel *regionLabel;
@end


@implementation GJNumberAnnotationCallOut

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.numberLabel];
    [self addSubview:self.regionLabel];
    [self.regionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(self).offset(6);
      make.right.equalTo(self).offset(-6);
      make.top.equalTo(self).offset(3.5 + 8);
      make.height.mas_equalTo(14);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(self).offset(6);
      make.right.equalTo(self).offset(-6);
      make.top.equalTo(self.regionLabel.mas_bottom).offset(2);
      make.bottom.equalTo(self).offset(-3.5 - 8);
    }];
  }
  return self;
}

#pragma mark -- setter
- (void)setNumberText:(NSString *)numberText{
  _numberText = numberText;
  NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:_numberText];
  [mutableString addAttribute:NSForegroundColorAttributeName value:themColor range:NSMakeRange(0, _numberText.length)];
  [mutableString addAttribute:NSFontAttributeName value:kMediumSystemFontOfSize(14) range:NSMakeRange(0, _numberText.length  -1)];
  [mutableString addAttribute:NSFontAttributeName value:kMediumSystemFontOfSize(11) range:NSMakeRange(_numberText.length - 1, 1)];
  self.numberLabel.attributedText = mutableString;
}
- (void)setRegionText:(NSString *)regionText{
  _regionText = regionText;
  self.regionLabel.text = _regionText;
}

#pragma mark -- getter
- (UILabel *)numberLabel{
  if (!_numberLabel) {
    _numberLabel = [UILabel new];
    _numberLabel.textColor = themColor;
    _numberLabel.font = kMediumSystemFontOfSize(14);
    _numberLabel.textAlignment = NSTextAlignmentCenter;
  }
  return _numberLabel;
}

- (UILabel *)regionLabel{
  if (!_regionLabel) {
    _regionLabel = [UILabel new];
    _regionLabel.textColor = HexColor(@"#666562");
    _regionLabel.font = kSystemFontOfSize(10);
    _regionLabel.textAlignment = NSTextAlignmentCenter;
  }
  return _regionLabel;
}


- (void)drawRect:(CGRect)rect {
  [super drawRect:rect];
  CGFloat strokeWidth = 3.5;
  CGFloat viewW = rect.size.width;
  CGFloat viewH = rect.size.height - Arror_height;
  CGFloat kBorderOffset = 1;
  CGFloat borderRadius = (viewH  - Arror_height ) * 0.5;
  CGFloat offset = strokeWidth + kBorderOffset;
  
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetLineJoin(context, kCGLineJoinRound); //
  CGContextSetLineWidth(context, strokeWidth); // 设置画笔宽度
  CGContextSetStrokeColorWithColor(context, themColor.CGColor); // 设置画笔颜色
  CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); // 设置填充颜色
  
  // 画三角形
  CGContextBeginPath(context);
  CGContextMoveToPoint(context, strokeWidth, borderRadius + strokeWidth);
  // 画其余部分
  CGContextAddArcToPoint(context, strokeWidth, viewH - strokeWidth, borderRadius, viewH - strokeWidth, borderRadius);
  CGContextAddLineToPoint(context, viewW * 0.5 - Arror_height, viewH -strokeWidth);
  CGContextAddLineToPoint(context, viewW * 0.5, viewH + Arror_height - strokeWidth);
  CGContextAddLineToPoint(context, viewW * 0.5 + Arror_height, viewH - strokeWidth);
  
  CGContextAddArcToPoint(context, viewW -  strokeWidth, viewH - strokeWidth, viewW - strokeWidth,viewH - borderRadius, borderRadius);
  CGContextAddArcToPoint(context, viewW - strokeWidth, strokeWidth, viewW-borderRadius, strokeWidth, borderRadius);
  CGContextAddArcToPoint(context, strokeWidth, strokeWidth, strokeWidth, borderRadius + strokeWidth, borderRadius);
  
  
  
  
  CGContextClosePath(context);
  CGContextDrawPath(context, kCGPathFillStroke);
  
  
  CGContextSetFillColorWithColor(context, themColor.CGColor); // 设置填充颜色
  CGContextMoveToPoint(context, viewW * 0.5 - Arror_height, viewH -strokeWidth);
  
  CGContextAddLineToPoint(context, viewW * 0.5, viewH + Arror_height - strokeWidth);
  CGContextAddLineToPoint(context, viewW * 0.5 + Arror_height, viewH - strokeWidth);
  CGContextAddLineToPoint(context, viewW * 0.5 - Arror_height, viewH -strokeWidth);
  CGContextClosePath(context);
  CGContextDrawPath(context, kCGPathFillStroke);
}
@end
